package com.hospital.ecard.patienthandler;

import com.hospital.common.core.module.controller.vo.PatientReqEntity;
import com.hospital.common.core.utils.IdentityPlace;
import com.hospital.common.core.utils.PinYinKit;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.ecard.module.entity.PatientInfo;
import com.hospital.ecard.module.service.PatientInfoService;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lvyao
 * @description 绑定任务执行
 * @date 2021/5/6
 */
public class PatientInfoExecute {

    PatientInfoElectronicHandler patientInfoHandler;

    @Resource
    private PatientInfoService patientInfoService = SpringContextHolder.getBean(PatientInfoService.class);

    public PatientInfoExecute(PatientInfoElectronicHandler patientInfoHandler) {
        this.patientInfoHandler = patientInfoHandler;
    }

    public PatientInfo execute(PatientReqEntity patientReqEntity) throws Exception {
        PatientInfo patientInfo= patientInfoHandler.patientHandleInfo(patientReqEntity);
        return commExecute(patientReqEntity, patientInfo);
    }

    private PatientInfo commExecute(PatientReqEntity patientReqEntity, PatientInfo patientInfo) {
        List<PatientInfo> patientInfoListClear = patientInfoService.selectPatientInfoByIdNo(patientReqEntity.getIdNo()).stream().filter(c -> !c.getPatientId().equals(patientInfo.getPatientId())).collect(Collectors.toList());
        if (patientInfo.getNamePhonetic() == null) {
            String pinY = PinYinKit.getHanziInitials(patientInfo.getName());
            patientInfo.setNamePhonetic(pinY.replace(':', ' ').toUpperCase());
        }
        if (patientInfo.getMailingAddress() == null) {
            if (patientReqEntity.getState() != null && patientReqEntity.getCity() != null && patientReqEntity.getAddress() != null) {
                patientInfo.setMailingAddress(patientReqEntity.getState() + patientReqEntity.getCity() + patientReqEntity.getAddress());
            }
        }
        if (!patientInfo.getName().equals(patientReqEntity.getPatientName())){
            patientInfo.setName(patientReqEntity.getPatientName());
            String pinY = PinYinKit.getHanziInitials(patientReqEntity.getPatientName());
            patientInfo.setNamePhonetic(pinY.replace(':', ' ').toUpperCase());
        }
        if (patientInfo.getPhoneNumberHome() != null && patientReqEntity.getPhone() != null && !patientReqEntity.getPhone().isEmpty() && patientInfo.getPhoneNumberHome() != patientReqEntity.getPhone()) {
            patientInfo.setPhoneNumberHome(patientReqEntity.getPhone());
        }
        if (patientReqEntity.getPhid() != null) {
            patientInfo.setPHid(patientReqEntity.getPhid());
        }
        if (patientReqEntity.getQrCodeText() != null) {
            patientInfo.setQrCodeText(patientReqEntity.getQrCodeText());
        }
        if (patientReqEntity.getAdminExt() != null) {
            patientInfo.setAdminExt(patientReqEntity.getAdminExt());
        }
        if (patientReqEntity.getHealthCardId() != null) {
            patientInfo.setHealthCardId(patientReqEntity.getHealthCardId());
        }
        if (StringUtils.isEmpty(patientInfo.getState()) || StringUtils.isEmpty(patientInfo.getCity()) || StringUtils.isEmpty(patientInfo.getDistrict())) {
            String s = patientReqEntity.getIdNo().substring(0, 6);
            String stateString = IdentityPlace.getProvince(Integer.parseInt(s));
            int length = stateString.length();
            patientInfo.setState(length > 5 ? stateString.substring(0, 5) : stateString);
            patientInfo.setCity(IdentityPlace.getCity(Integer.parseInt(s)));
            patientInfo.setDistrict(IdentityPlace.getDistrict(Integer.parseInt(s)));
        }
        if (StringUtils.isEmpty(patientInfo.getAddress())) {
            patientInfo.setAddress(patientReqEntity.getAddress());
        }
        if (patientInfo.getIdNo() == null || patientInfo.getIdNo().isEmpty()){
            patientInfo.setIdNo(patientReqEntity.getIdNo());
        }
        if (patientInfoListClear != null && patientInfoListClear.size() > 0) {
            for (PatientInfo patientInfoClear : patientInfoListClear) {
                patientInfoClear.setHealthCardId("");
                patientInfoClear.setPHid("");
                patientInfoClear.setQrCodeText("");
                patientInfoClear.setAdminExt("");
                patientInfoClear.setVirtualCardNum("");
                patientInfoService.updateById(patientInfoClear);
            }
        }
        if (patientReqEntity.getDataOper() > 0) {
            patientInfoService.save(patientInfo);
        } else {
            patientInfoService.updateById(patientInfo);
        }

        return patientInfo;
    }
}
